package StudyCode.summary.myfile;

import java.nio.file.attribute.PosixFilePermission;
import java.util.HashSet;
import java.util.Set;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class GetFilePermissions {
	public static void main(String[] argv) {
		int mode = 2;
		System.out.println(getPermissions(mode));
	}


	private static Set<PosixFilePermission> getPermissions(int mode) {
		Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
		if ((mode & 0400) > 0) {
			perms.add(PosixFilePermission.OWNER_READ);
		}
		if ((mode & 0200) > 0) {
			perms.add(PosixFilePermission.OWNER_WRITE);
		}
		if ((mode & 0100) > 0) {
			perms.add(PosixFilePermission.OWNER_EXECUTE);
		}

		if ((mode & 0040) > 0) {
			perms.add(PosixFilePermission.GROUP_READ);
		}
		if ((mode & 0020) > 0) {
			perms.add(PosixFilePermission.GROUP_WRITE);
		}
		if ((mode & 0010) > 0) {
			perms.add(PosixFilePermission.GROUP_EXECUTE);
		}

		if ((mode & 0004) > 0) {
			perms.add(PosixFilePermission.OTHERS_READ);
		}
		if ((mode & 0002) > 0) {
			perms.add(PosixFilePermission.OTHERS_WRITE);
		}
		if ((mode & 0001) > 0) {
			perms.add(PosixFilePermission.OTHERS_EXECUTE);
		}
		return perms;
	}
}
